home *** CD-ROM | disk | FTP | other *** search
- #include "Recognition.h"
-
- OSErr MakeNewLanguage (SpeechInfoPtr theSpeechInfo)
- {
- OSErr theErr = noErr;
-
- /* Make language models */
- theErr = ReadInLanguages (theSpeechInfo);
-
- /* Use this LM in recognition */
- if (theErr == noErr) {
- theErr = SRSetLanguageModel (theSpeechInfo->theRecognizer, theSpeechInfo->languages->theLanguage);
- }
-
- return theErr;
- }
-
- OSErr ReadInLanguages (SpeechInfoPtr theSpeechInfo)
- {
- LanguageModelListPtr currentLang = nil;
- TREEResourcePtr TREEResPtr = nil;
- short numLanguages = 0,
- onlySubPath = false,
- i;
- OSErr theErr = noErr;
-
- theErr = ResError ();
-
- theSpeechInfo->languages = (LanguageModelListPtr)NewPtr (sizeof (LanguageModelList));
- theErr = AddStringsToLanguage (theSpeechInfo, currentLang, kBaseResID + i);
-
- return theErr;
- }
-
-
- OSErr AddStringsToLanguage (SpeechInfoPtr theSpeechInfo, LanguageModelListPtr currentLang, short resID)
- {
- SRLanguageModel newModel;
- SRPath beginningOfPath,
- restOfPath;
- Str63 phraseString;
- OSType type = 0;
- TREEResourcePtr TREEResPtr = nil;
- CommandPtr theCommand = nil;
- long refCon = 0,
- flags = 0,
- ID = 0;
- short j = 1,
- onlySubPath = false;
- OSErr theErr = noErr;
- Boolean done = false;
-
- theErr = SRNewLanguageModel (theSpeechInfo->recogSystem, &newModel, nil, 0);
- refCon = resID;
- theErr = SRSetProperty (newModel, kSRRefCon, &refCon, sizeof (refCon));
- theErr = SRAddText (newModel, phraseString+1, phraseString[0], (long)theCommand);
-
- if (theErr != noErr) { /* release newly created LM if an error occured while adding phrases */
- SRReleaseObject (newModel);
- }
- else { /* return new LM */
- //We don't release the language models because we will be switching between them later
- currentLang->theLanguage = newModel;
- }
-
- return theErr;
- }
-
- OSErr InitSpeechRecognition (SpeechInfoPtr theSpeechInfo)
- {
- OSErr theErr = noErr;
-
- /* Make sure SpeechRecognition Toolbox is available */
- theErr = Gestalt (gestaltSpeechRecognitionVersion, &theSpeechInfo->attributes);
- /* Version number must be at least 1.5 to support Speech Recognition Toolbox API used here. */
- if (theErr == noErr) {
- if (theSpeechInfo->attributes < 0x00000150) {
- theErr = kNotEnoughSpeechVersion;
- }
- }
-
- return theErr;
- }
-
- OSErr OpenSpeechRecognition (SpeechInfoPtr theSpeechInfo)
- {
- OSErr theErr = noErr;
-
- theErr = SROpenRecognitionSystem (&theSpeechInfo->recogSystem, kSRDefaultRecognitionSystemID);
-
- return theErr;
- }
-
- OSErr ConfigureRecognition (SpeechInfoPtr theSpeechInfo)
- {
- OSErr theErr = noErr;
- short feedbackModes = kSRHasFeedbackHasListenModes;
-
- /* We use the default feedback and listening modes */
- theErr = SRSetProperty (theSpeechInfo->recogSystem, kSRFeedbackAndListeningModes, &feedbackModes, sizeof (feedbackModes));
-
- return theErr;
- }
-
- OSErr GetNewRecognizer (SpeechInfoPtr theSpeechInfo)
- {
- OSErr theErr = noErr;
- Boolean forground = false; //We want to recognize even if in the background
-
- /* Create a recognizer with default speech source -- e.g. the desktop microphone */
- theErr = SRNewRecognizer (theSpeechInfo->recogSystem, &theSpeechInfo->theRecognizer, kSRDefaultSpeechSource);
- if (theErr == noErr) {
- theErr = SRSetProperty (theSpeechInfo->theRecognizer, kSRForegroundOnly, &forground, sizeof (forground));
- }
-
- return theErr;
- }
-